// pages/my/index/index.js
import regeneratorRuntime from '../../utils/regeneratorRuntime.js'
import { data } from '../../utils/data.js'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    integral: '',
    myList: data.myList,
    moduleList: [
      {
        name: '全部订单',
        url: '../my/order/index'
      }, {
        name: '收货地址',
        url: '../my/address/index'
      }, {
        name: '积分明细',
        url: '/pages/column/integral/bill'
      }, 
      // {
      //   name: '中奖记录',
      //   url: ''
      // },
      {
        name: '活动中心',
        url: '/pages/column/activity/index'
      }, {
        name: '积分任务',
        url: '/pages/column/task/index'
      }, {
        name: '周边商家',
        url: '../my/near-shop/index'
      }, {
        name: '投诉建议',
        url: '../my/feedback/index',
      }
      // }, {
      //   name: '关于我们',
      //   url: ''
      // }
    ],
    orderIndex: 0,
    orderList: [
      {
        name: '待收货/使用',
        icon: '/picture/my/1.png',
        url: `../my/order/index?index=1`,
        num: 0
      }, {
        name: '已收货/使用',
        icon: '/picture/my/2.png',
        url: `../my/order/index?index=2`,
        num: 0
      }, {
        name: '售后',
        icon: '/picture/my/3.png',
        url: `../my/order/index?index=3`,
        num: 0
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getIndex()
    this.getOrder()
  },

  async getIndex() {
    try {
      const { user } = await app.index.getUserIndex()
      this.setData({
        integral: user.credits,
        user
      })
    } catch (err) {
      console.log(err)
      app.toast(err)
    }
  },


  getOrder() {
    let orderList = this.data.orderList
    Promise.all([
      app.order.getGoodsOrders(1, 1)
    ]).then(list => {
      list.forEach((item, index) => {
        orderList[index].num = item.data.length
      })
      this.setData({
        orderList
      })
    })
  },

  linkTap(e) {
    let url = e.currentTarget.dataset.link
    if (url) {
      wx.navigateTo({
        url: url,
      })
    }
  },



  onSign() {
    wx.navigateTo({
      url: '/pages/column/sign/index'
    })
  },

  onToPersonal() {
    let user = JSON.stringify(this.data.user)
    wx.navigateTo({
      url: `/pages/my/personal/index?user=${user}`
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})



